package com.heyqing.topic.HammingWeight_0191;

/**
 * ClassName:HammingWeight
 * Package:com.heyqing.topic.HammingWeight_0191
 * Description:
 *
 * @Date:2024/9/20
 * @Author:Heyqing
 */
public class HammingWeight {
    public int hammingWeight(int n) {
        int counts = 0;
        //因为int为4byte即2^32，故须循环32次
        for (int i = 0; i < 32; i++) {
            if (((n >> i) & 1) == 1) {
                counts++;
            }
        }
        return counts;
    }
}
